

const getPoseArrAndErrorCode = (result) => {
  const errorCodes = [];
  const postures = [];
  const paramNames = [
    "top",
    "neck",
    
    "shoulder_r",
    "elbow_r",
    "wrist_r",
    "shoulder_l",
    "elbow_l",
    "wrist_l",
    
    "hip_r",
    "knee_r",
    "ankle_r",
    "hip_l",
    "knee_l",
    "ankle_l",
  ];
  result.forEach(item => {
    if (item.errorCode !== '500') {
      errorCodes.push(item.errorCode);
      const posturesArr = []; 
      paramNames.forEach(pName => {
        const param = item[pName];
        if (param) {
          const resultArr = param.substr(1, param.length - 2).split(',');
          posturesArr.push(...resultArr.map(itemStr => parseFloat(itemStr)));
        } else {
          return posturesArr.push(...[999, 999]);
        }
      })
      postures.push(posturesArr);
    }
  });
  return { postures, errorCodes };
}

const getFrontPoseArrAndErrorCode = (result) => {
  const errorCodes = [];
  const postures = [];
  const paramNames = [
    "top",
    "neck",
    
    "shoulder_r",
    "elbow_r",
    "wrist_r",
    "shoulder_l",
    "elbow_l",
    "wrist_l",
    
    "hip_r",
    "knee_r",
    "ankle_r",
    "hip_l",
    "knee_l",
    "ankle_l",
  ];
  result.forEach(item => {
    if (item.errorCode !== '500') {
      const rightErrorCode = {
        '0': '0',
        '1': '1',
        '201': '2',
        '200': '3',
        '211': '4',
        '221': '5',
      }
      errorCodes.push(rightErrorCode[item.errorCode]);
      const posturesArr = []; 
      paramNames.forEach(pName => {
        const param = item[pName];
        if (param) {
          const resultArr = param.substr(1, param.length - 2).split(',');
          posturesArr.push(...resultArr.map(itemStr => parseFloat(itemStr)));
        } else {
          return posturesArr.push(...[999, 999]);
        }
      })
      postures.push(posturesArr);
    }
  });
  return { postures, errorCodes };
}

const getNameAndValue = (requestBody) => {
  let nameStr = '';
  let valueStr = ''
  for (const key in requestBody) {
    if (requestBody.hasOwnProperty(key) && requestBody[key]) {
      nameStr += `\`${key}\`,`;
      valueStr += `'${requestBody[key]}',`;
    }
  }
  return { nameStr, valueStr };
}

module.exports = { getNameAndValue, getPoseArrAndErrorCode, getFrontPoseArrAndErrorCode };